﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Aspose.Cells;
using CaseReivew.Model;
using CaseReview.SQLServerDAL;
using CaseReview.Utility;

public partial class admin_result : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack) {
        IList<ReviewResultInfo> results = BusinessLogic.GetInstance().GetTotalResultsByStatus(0);
        List<int> cac = new List<int>();
        List<int> bac = new List<int>();
        List<string> nce = new List<string>();
        List<string> os = new List<string>();
        Dictionary<int, string> dictionary = new Dictionary<int, string>();
        dictionary.Add(0, "100%");
        dictionary.Add(1, "90%");
        dictionary.Add(2, "80%");
        dictionary.Add(3, "70%");
        dictionary.Add(4, "60%");
        dictionary.Add(5, "50%");
        dictionary.Add(6, "40%");
        dictionary.Add(7, "30%");
        dictionary.Add(8, "20%");
        dictionary.Add(9, "10%");
        dictionary.Add(10, "0%");
        foreach (ReviewResultInfo reviewResultInfo in results)
        {
            int count = 0;
            if (reviewResultInfo.Cac1.Equals("UnMeet"))
                count++;
            if (reviewResultInfo.Cac2.Equals("UnMeet"))
                count++;
            if (reviewResultInfo.Cac3.Equals("UnMeet"))
                count++;
            cac.Add(count);
            count = 0;
            if (reviewResultInfo.Bac1.Equals("UnMeet"))
                count++;
            if (reviewResultInfo.Bac2.Equals("UnMeet"))
                count++;
            if (reviewResultInfo.Bac3.Equals("UnMeet"))
                count++;
            bac.Add(count);
            count = 0;
            if (reviewResultInfo.Nce1.Equals("UnMeet"))
                count++;
            if (reviewResultInfo.Nce2.Equals("UnMeet"))
                count++;
            if (reviewResultInfo.Nce3.Equals("UnMeet"))
                count++;
            if (reviewResultInfo.Nce4.Equals("UnMeet"))
                count++;
            if (reviewResultInfo.Nce5.Equals("UnMeet"))
                count++;
            if (reviewResultInfo.Nce6.Equals("UnMeet"))
                count++;
            if (reviewResultInfo.Nce7.Equals("UnMeet"))
                count++;
            if (reviewResultInfo.Nce8.Equals("UnMeet"))
                count++;
            if (reviewResultInfo.Nce9.Equals("UnMeet"))
                count++;
            if (reviewResultInfo.Nce10.Equals("UnMeet"))
                count++;
            nce.Add(dictionary[count]);
        }
        for (int i = 0; i < results.Count; i++)
        {
            if ((cac[i] == 0) && (bac[i] == 0) && (nce[i] == "70%" || nce[i] == "80%" || nce[i] == "90%" || nce[i] == "100%"))
            {
                os.Add("Pass");
            }
            else
            {
                os.Add("Fail");
            }
        }
            ViewState["cac"] = cac;
            ViewState["bac"] = bac;
            ViewState["nce"] = nce;
            ViewState["os"] = os;
            BindData();
        }
     
        
    }

    private void BindData()
    {
        IList<ReviewResultInfo> results = BusinessLogic.GetInstance().GetTotalResultsByStatus(0);
        GridView1.DataSource = results;
        GridView1.DataKeyNames = new string[] { "CaseId" };
        GridView1.DataBind();
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        BindData();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
        List<int> cac = ViewState["cac"] as List<int>;
        List<int> bac = ViewState["bac"] as List<int>;
        List<string> nce = ViewState["nce"] as List<string>;
        List<string> os = ViewState["os"] as List<string>;
        if(e.Row.RowIndex > -1) {
            e.Row.Cells[4].Text = cac[e.Row.RowIndex].ToString();
            e.Row.Cells[5].Text = bac[e.Row.RowIndex].ToString();
            e.Row.Cells[6].Text = nce[e.Row.RowIndex];
            e.Row.Cells[7].Text = os[e.Row.RowIndex];
        }
    }
    protected void btnExport_Click(object sender, EventArgs e)
    {
        IList<ReviewResultInfo> results = BusinessLogic.GetInstance().GetTotalResultsByStatus(0);
        DataTable dataTable = CreateDataTable(results);
        DownLoadExcel(dataTable,"Review Summary");
        
    }

    public DataTable CreateDataTable(IList<ReviewResultInfo> list) {
        DataTable dt = new DataTable("Result");
        DataColumn dc;
        DataRow dr;
        dc = new DataColumn();
        dc.DataType = Type.GetType("System.String");
        dc.ColumnName = "Case ID";
        dt.Columns.Add(dc);
        dc = new DataColumn();
        dc.DataType = Type.GetType("System.String");
        dc.ColumnName = "Case Owner";
        dt.Columns.Add(dc);
        dc = new DataColumn();
        dc.DataType = Type.GetType("System.String");
        dc.ColumnName = "Reviewer";
        dt.Columns.Add(dc);
        dc = new DataColumn();
        dc.DataType = Type.GetType("System.DateTime");
        dc.ColumnName = "Review Date";
        dt.Columns.Add(dc);      
        dc = new DataColumn();
        dc.DataType = Type.GetType("System.Int32");
        dc.ColumnName = "Customer Affecting Critical Error";
        dt.Columns.Add(dc);
        dc = new DataColumn();
        dc.DataType = Type.GetType("System.Int32");
        dc.ColumnName = "Business Affecting Critical Error";
        dt.Columns.Add(dc);
        dc = new DataColumn();
        dc.DataType = Type.GetType("System.String");
        dc.ColumnName = "Non-Critical Error %";
        dt.Columns.Add(dc);
        dc = new DataColumn();
        dc.DataType = Type.GetType("System.String");
        dc.ColumnName = "Overall Score";
        dt.Columns.Add(dc);
        dc = new DataColumn();
        dc.DataType = Type.GetType("System.String");
        dc.ColumnName = "Scope";
        dt.Columns.Add(dc);
        dc = new DataColumn();
        dc.DataType = Type.GetType("System.String");
        dc.ColumnName = "Technical";
        dt.Columns.Add(dc);
        dc = new DataColumn();
        dc.DataType = Type.GetType("System.String");
        dc.ColumnName = "Understand customer";
        dt.Columns.Add(dc);
        dc = new DataColumn();
        dc.DataType = Type.GetType("System.String");
        dc.ColumnName = "Meet SLA";
        dt.Columns.Add(dc);
        dc = new DataColumn();
        dc.DataType = Type.GetType("System.String");
        dc.ColumnName = "Respond in 24h";
        dt.Columns.Add(dc);
        dc = new DataColumn();
        dc.DataType = Type.GetType("System.String");
        dc.ColumnName = "Strike";
        dt.Columns.Add(dc);
        dc = new DataColumn();
        dc.DataType = Type.GetType("System.String");
        dc.ColumnName = "Research";
        dt.Columns.Add(dc);
        dc = new DataColumn();
        dc.DataType = Type.GetType("System.String");
        dc.ColumnName = "Consult and escalate";
        dt.Columns.Add(dc);
        dc = new DataColumn();
        dc.DataType = Type.GetType("System.String");
        dc.ColumnName = "Communication";
        dt.Columns.Add(dc);
        dc = new DataColumn();
        dc.DataType = Type.GetType("System.String");
        dc.ColumnName = "Log labor";
        dt.Columns.Add(dc);
        dc = new DataColumn();
        dc.DataType = Type.GetType("System.String");
        dc.ColumnName = "Log research";
        dt.Columns.Add(dc);
        dc = new DataColumn();
        dc.DataType = Type.GetType("System.String");
        dc.ColumnName = "Non-tech template";
        dt.Columns.Add(dc);
        dc = new DataColumn();
        dc.DataType = Type.GetType("System.String");
        dc.ColumnName = "English";
        dt.Columns.Add(dc);
        dc = new DataColumn();
        dc.DataType = Type.GetType("System.String");
        dc.ColumnName = "Manage expectation";
        dt.Columns.Add(dc);
        dc = new DataColumn();
        dc.DataType = Type.GetType("System.String");
        dc.ColumnName = "Address non-tech concerns";
        dt.Columns.Add(dc);
        dc = new DataColumn();
        dc.DataType = Type.GetType("System.String");
        dc.ColumnName = "Manage case data";
        dt.Columns.Add(dc);
        dc = new DataColumn();
        dc.DataType = Type.GetType("System.String");
        dc.ColumnName = "Summary";
        dt.Columns.Add(dc);
        dc = new DataColumn();
        dc.DataType = Type.GetType("System.String");
        dc.ColumnName = "Suggestion";
        dt.Columns.Add(dc);

        int i = 0;
        List<int> cac = ViewState["cac"] as List<int>;
        List<int> bac = ViewState["bac"] as List<int>;
        List<string> nce = ViewState["nce"] as List<string>;
        List<string> os = ViewState["os"] as List<string>;

        foreach (ReviewResultInfo resultInfo in list) {
            dr = dt.NewRow();
            dr[0] = resultInfo.CaseId;
            dr[1] = resultInfo.Owner;
            dr[2] = resultInfo.Reviewer;
            dr[3] = resultInfo.Date;
            dr[4] = cac[i];
            dr[5] = bac[i];
            dr[6] = nce[i];
            dr[7] = os[i];
            dr[8] = resultInfo.Cac1;
            dr[9] = resultInfo.Cac2;
            dr[10] = resultInfo.Cac3;
            dr[11] = resultInfo.Bac1;
            dr[12] = resultInfo.Bac2;
            dr[13] = resultInfo.Bac3;
            dr[14] = resultInfo.Nce1;
            dr[15] = resultInfo.Nce2;
            dr[16] = resultInfo.Nce3;
            dr[17] = resultInfo.Nce4;
            dr[18] = resultInfo.Nce5;
            dr[19] = resultInfo.Nce6;
            dr[20] = resultInfo.Nce7;
            dr[21] = resultInfo.Nce8;
            dr[22] = resultInfo.Nce9;
            dr[23] = resultInfo.Nce10;
            dr[24] = resultInfo.Osum;
            dr[25] = resultInfo.Osug;
            dt.Rows.Add(dr);
            i++;
        }
        return dt;
    }


    public void DownLoadExcel(DataTable dt, string fileName)
    {
        Workbook workbook = new Workbook();
        Worksheet sheet = workbook.Worksheets[0];
        sheet.FreezePanes(1,1,1,0);
        AddHeader(sheet,dt);
        AddBody(sheet,dt);
        sheet.AutoFitColumns();
        HttpResponse response = HttpContext.Current.Response;
        workbook.Save(DateTime.Now.ToShortDateString() + "_" + fileName + ".xlsx",SaveType.OpenInBrowser,FileFormatType.Excel2007Xlsx,response);
    }

    private void AddHeader(Worksheet sheet, DataTable dt)
    {
        Cell cell = null;
        for (int col = 0; col < dt.Columns.Count; col++)
        {
            cell = sheet.Cells[0, col];
            cell.PutValue(dt.Columns[col].ColumnName);
            cell.Style.Font.IsBold = true;
        }
    }

    private void AddBody(Worksheet sheet,DataTable dt)
    {
        for (int r = 0; r < dt.Rows.Count; r++)
        {
            for (int c = 0; c < dt.Columns.Count; c++)
            {
                sheet.Cells[r + 1, c].PutValue(dt.Rows[r][c].ToString());
            }
        }
    }

    
}